home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: quick decision: is n a power of 2?
- Date: 20 Jan 1996 05:02:22 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan19220222@qcd.lanl.gov>
- References: <4dorr8$i58@cloner3.netcom.com> <4dp2no$3va@unix.sri.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: mklenk@updike.sri.com's message of 19 Jan 1996 21:33:44 GMT
-
- --text follows this line--
- In article <4dp2no$3va@unix.sri.com> mklenk@updike.sri.com (Mark
- Klenk) writes:
- <snip>
- >>
- >>Is there a fast way to decide whether a number n is a power of 2?
- >
- >no tricks
- >
- >#define ISPOW2(x) (((x) & 1) ^ 1)
-
- He asked for POWER of 2, not MULTIPLE.
-
- Your macro will return non-zero for 6, for example,
- which is not a power of 2.
-
- I don't think there is any way to do it other than
- recursively or iteratively.
-
- Don't think, try
-
- #define POWER(x) (!((x) & ((x)-1)))
-
- on unsigned types.
-
- Cheers
- Tanmoy
-
-
-
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-